#include "custommodel.h"
#include <ctime>
#include <algorithm>
#include <QDebug>
#include <QColor>
CustomModel::CustomModel(QObject *parent) : QAbstractItemModel(parent)
{
    QStringList tabHeaderList;
    tabHeaderList<<"文件名字"
                <<"路劲"
               <<"打开";
    m_btnColumn=2;
    this->fun_setHeaderData(tabHeaderList);
    m_data.clear();
    m_names.clear();
}

void CustomModel::fun_setHeaderData(QStringList names)
{
    m_names=names;
}

void CustomModel::fun_addDataRow(_DATA_STRUCT data)
{
    m_data.append(data);
}

void CustomModel::fun_clearData()
{
    m_data.clear();
    this->removeRows(0,this->rowCount());
}

void CustomModel::fun_HederInit()
{
    QStringList tabHeaderList;

        tabHeaderList<<"序号"
                    <<"文件名"
                   <<"打开";
    m_btnColumn=2;
    this->fun_setHeaderData(tabHeaderList);
}

_DATA_STRUCT CustomModel::fun_GetDataItem(QModelIndex index)
{
    if(index.isValid()!=false&&index.row()<m_data.count()){
       return m_data[index.row()];
    }
    return _DATA_STRUCT();
}

QList<_DATA_STRUCT> CustomModel::fun_GetListAll()
{
    return m_data;
}

int CustomModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_data.length();
}
int CustomModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_names.length();
}

QVariant CustomModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid()){
        return QVariant();
    }
    int rowCount=index.row();
    int columnCount=index.column();

    if(rowCount>=m_data.count()||columnCount>=m_names.count()){
        return QVariant();
    }
    if(role == Qt::DisplayRole||role == Qt::EditRole){
        //获取行中的列数据并返回
        if(columnCount==0){
            return rowCount+1;
        }else{
            _DATA_STRUCT data=m_data[rowCount];
            switch (columnCount) {
            case 1:
                return data.strFileName;
                break;
            case 2:
                return data.strPath;
                break;
            default:
                break;
            }
        }
    }
    if(role == Qt::TextAlignmentRole)
    {
        if(columnCount<=1){
            return QVariant(Qt::AlignLeft);
        }else{
            return QVariant(Qt::AlignCenter);
        }
    }
    if(role == Qt::TextColorRole&&columnCount==m_btnColumn)
    {
        return QColor(255,255,255);
    }
    if(role == Qt::DecorationRole&&index.column()==1)
    {
        return m_data[rowCount]._Icon;
    }

    return QVariant();
}

bool CustomModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if(!index.isValid()){
        return false;
    }
    if(index.row()>=m_data.count()||index.column()>=m_names.count()){
        return false;
    }
    return false;
}

QVariant CustomModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(role != Qt::DisplayRole){
        return QVariant();
    }

    if(orientation == Qt::Horizontal){
        if(section < 0 || section > m_names.length()){return  QVariant();}
        return QVariant(m_names.at(section));
    }else {
        return QVariant(QString::number(section));
    }
    return QVariant();
}
bool CustomModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
{
    if(role != Qt::DisplayRole){
        return false;
    }
    //判断是水平表头还是垂直表头
    if(orientation != Qt::Horizontal){
        return false;
    }else{
        if(section < 0||section >= m_names.length()){
            return  false;
        }
        if(section>=0&&section<m_names.length()){
            m_names.replace(section, value.toString());
            emit headerDataChanged(Qt::Horizontal, section, section);//发出信号让视图调用并刷新界面
        }
        return true;
    }
}

Qt::ItemFlags CustomModel::flags(const QModelIndex &index) const
{
    if(!index.isValid()){
        return QAbstractItemModel::flags(index);
    }

    int rowCount=index.row();
    int columnCount=index.column();
    if(rowCount>=m_data.count()||columnCount>=m_names.count()){
        return QAbstractItemModel::flags(index);
    }
    Qt::ItemFlags flags;        
    if(columnCount==m_btnColumn){
        flags = Qt::ItemIsEnabled | Qt::ItemNeverHasChildren ;
        return flags;
    }else{
         flags=Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren;
         return flags;
     }
    return QAbstractItemModel::flags(index);
}

QModelIndex CustomModel::index(int row, int column, const QModelIndex &parent) const
{
    //列不能超过父的列数，也不能超过指定的列数，不能小于零，行不能小于零，不能超过有效的函数
    if (row < 0 || column < 0 || column >= columnCount(parent) || column > m_names.length()){
        return QModelIndex();
    }
    return createIndex(row,column);
}

QModelIndex CustomModel::parent(const QModelIndex &child) const
{
    Q_UNUSED(child);
    return QModelIndex();
}
